﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using InhollandAir.auxiliary;

namespace InhollandAir
{
    public partial class ChooseFlight : UserControl
    {
        public static Country CountryFrom { get; set; }
        public static Country CountryTo { get; set; }
        public static Airport AirportFrom { get; set; }
        public static Airport AirportTo { get; set; }

        public delegate void PropertyChanged(object o);

        public event PropertyChanged DidChangeCountryFrom;
        public event PropertyChanged DidChangeAirportFrom;
        public event PropertyChanged DidChangeCountryTo;
        public event PropertyChanged DidChangeAirportTo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlCountryFrom.Items.Add(new ListItem("Select country ...", "-1"));
                ddlCountryTo.Items.Add(new ListItem("Select country ...", "-1"));

                ddlAirportFrom.Items.Add(new ListItem("Select airport ...", "-1"));
                ddlAirportTo.Items.Add(new ListItem("Select airport ...", "-1"));

                ddlAirportFrom.Enabled = false;
                ddlAirportTo.Enabled = false;

                // Set all countries in DropDownLists
                foreach (var country in Sql.GetCountries())
                {
                    ddlCountryFrom.Items.Add(new ListItem(country.Name,
                                                          country.Id.ToString(CultureInfo.InvariantCulture)));
                    ddlCountryTo.Items.Add(new ListItem(country.Name, country.Id.ToString(CultureInfo.InvariantCulture)));
                }
            }
        }

        public void DdlCountryFromOnSelectedIndexChanged(object sender, EventArgs eventArgs)
        {
            int id;
            try
            {
                id = int.Parse(ddlCountryFrom.SelectedValue);
            }
            catch (FormatException ex)
            {
                return;
            }
            catch
            {
                return;
            }

            if(id < 0)
            {
                CountryFrom = null;
                return;
            }


            CountryFrom = Sql.GetCountryWithId(id);

            if (DidChangeCountryFrom != null)
                DidChangeCountryFrom(CountryFrom);

            ddlAirportFrom.Items.Clear();
            ddlAirportFrom.Items.Add(new ListItem("Select airport ...", "-1"));
            ddlAirportFrom.Enabled = true;
            
            foreach (var airport in Sql.GetAirportsForCountry(id))
                ddlAirportFrom.Items.Add(new ListItem(airport.Name, airport.Id.ToString(CultureInfo.InvariantCulture)));
        }

        public void DdlCountryToOnSelectedIndexChanged(object sender, EventArgs eventArgs)
        {
            int id;
            try
            {
                id = int.Parse(ddlCountryTo.SelectedValue);
            }
            catch (FormatException ex)
            {
                return;
            }
            catch
            {
                return;
            }

            if (id < 0) 
            {
                CountryTo = null;
                return;
            }

            CountryTo = Sql.GetCountryWithId(id);

            if (DidChangeCountryTo != null)
                DidChangeCountryFrom(CountryTo);

            ddlAirportTo.Items.Clear();
            ddlAirportTo.Items.Add(new ListItem("Select airport ...", "-1"));
            ddlAirportTo.Enabled = true;

            foreach (var airport in Sql.GetAirportsForCountry(id))
                ddlAirportTo.Items.Add(new ListItem(airport.Name, airport.Id.ToString(CultureInfo.InvariantCulture)));
        }

        public void DdlAirportFromOnSelectedIndexChanged(object sender, EventArgs e)
        {
            int id;
            try
            {
                id = int.Parse(ddlAirportFrom.SelectedValue);
            }
            catch (FormatException ex)
            {
                return;
            }
            catch
            {
                return;
            }

            if(id < 0)
            {
                AirportFrom = null;
                return;
            }


            AirportFrom = Sql.GetAirportWithId(id);

            if (DidChangeAirportFrom != null)
                DidChangeAirportFrom(AirportFrom);

        }

        public void DdlAirportToOnSelectedIndexChanged(object sender, EventArgs e)
        {
            int id;
            try
            {
                id = int.Parse(ddlAirportTo.SelectedValue);
            }
            catch (FormatException ex)
            {
                return;
            }
            catch
            {
                return;
            }

            if (id < 0)
            {
                AirportTo = null;
                return;
            }

            AirportTo = Sql.GetAirportWithId(id);

            if (DidChangeAirportTo != null)
                DidChangeAirportTo(AirportTo);
        }
    }
}